/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jaiz.twitter.model;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Marin
 */
public class DBManager {

    private Connection connection; //Objeto que representa y almacena la conexion actual con la Base de Datos
    private String userName;
    private String password;
    private String dataBase;
    private String server; //IP del servidor
    private String driver;
    private String url;
    
    private Properties properties;

    public DBManager() {
        properties = new Properties();
        try {
            properties.load(DBManager.class.getClassLoader().getResourceAsStream("../../properties/db.properties"));
        } catch (IOException ex) {
            Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        userName = properties.getProperty("db.username");
        password = properties.getProperty("db.password");
        dataBase = properties.getProperty("db.database");
        driver = properties.getProperty("db.driver");
        server = properties.getProperty("db.server");
        url = properties.getProperty("db.url");
        openConnection();
    }

    public PreparedStatement createStatement(String query) {
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(query);
        } catch (SQLException ex) {
            Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return preparedStatement;
    }
    
    public boolean commit(){
        boolean result = false;
        try {
            connection.commit();
            result = true;
        } catch (SQLException ex) {
            result = false;
        }finally{
            return result;
        }
    }
    
    public boolean rollback(){
        boolean result = false;
        try {
            connection.rollback();
            result = true;
        } catch (SQLException ex) {
            result = false;
        }finally{
            return result;
        }
    }
    
    public final boolean openConnection(){
        try {
            Class.forName(driver); //Cargamos el driver para trabajar con MySql
            connection = DriverManager.getConnection(url+server+"/"+dataBase, userName, password);
            System.out.println("*******CONECTADO A LA BASE DE DATOS**************");
            return true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (SQLException ex) {
             Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
    public final boolean closeConnection(){
        try {
            connection.close();
            return true;
        } catch (SQLException ex) {
            return false;
        }
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public String getDataBase() {
        return dataBase;
    }

    public void setDataBase(String dataBase) {
        this.dataBase = dataBase;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    
}
